package cn.jiyun.controller;

import cn.jiyun.entity.PageResult;
import cn.jiyun.entity.QueryPageBean;
import cn.jiyun.entity.Result;
import cn.jiyun.pojo.Student;
import cn.jiyun.service.StudentService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Reference
    private StudentService studentService;

    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_TEST')")
    @RequestMapping("findPage")
    public PageResult findPage (@RequestBody QueryPageBean queryPageBean){
        return studentService.findPage(queryPageBean);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_TESTa')")
    @RequestMapping("add")
    public Result add (@RequestBody Student student,Integer[] checkitemIds){
        try {

            studentService.add(student,checkitemIds);

            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }

    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_TESTa')")
    @RequestMapping("edit")
    public Result edit (@RequestBody Student student,Integer[] checkitemIds){
        try {

            studentService.edit(student,checkitemIds);

            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }

    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_TESTa')")
    @RequestMapping("delete")
    public Result delete (Integer id){
        try {

            studentService.delete(id);

            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }

    @RequestMapping("findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId (Integer id){
        try {

           List<Integer> list =  studentService.findCheckItemIdsByCheckGroupId(id);

            return new Result(true,"成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
 }
